package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @PostMapping
    @ApiOperation("新增套餐")
    //可以精准的清除对应的缓存数据（根据id到redis删除缓存）
    @CacheEvict(cacheNames = "setMealCache",key = "#setmealDTO.categoryId")
    public Result<String> insertSetMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.insertSetMeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult setmeals =setMealService.setMealPage(setmealPageQueryDTO);
        return Result.success(setmeals);
    }

    @DeleteMapping
    @ApiOperation("删除套餐")
    //这里是删除一条或多条数据，所以难以精确的清除缓存，所以清除所有的缓存
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result<String> deleteSetMeal(@RequestParam List<Integer> ids){
        setMealService.deleteSetMeal(ids);
        return Result.success();
    }

    @GetMapping("/{setmealId}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> querySetMealId(@PathVariable Long setmealId){
        SetmealVO setmealVO =setMealService.querySetMealId(setmealId);
        return Result.success(setmealVO);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result<String> updateSetMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("起售停售套餐")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result<String> ActivateAndForbiddenSetMeal(@PathVariable Integer status,Integer id){
        setMealService.ActivateAndForbiddenSetMeal(status,id);
        return Result.success();
    }

}
